package com.yanqu.road.dao.impl.activity.unionwar;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.unionwar.UnionQunyingTimeInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 赛制配置
 */
public class UnionQunyingTimeInfoDaoImpl extends TempDao {


    public static String getUnionQunyingTimeInfoSql = "select * from t_s_activity_qunying_time where activityId = ? order by timeId";

    public List<UnionQunyingTimeInfo> getUnionQunyingTimeInfo(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UnionQunyingTimeInfo> result = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getUnionQunyingTimeInfoSql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionQunyingTimeInfo unionQunyingTimeInfo = initUnionQunyingTimeInfo(rs);
                result.add(unionQunyingTimeInfo);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public Map<Integer, List<UnionQunyingTimeInfo>> getUnionQunyingTimeInfoMap(List<Integer> activityIdList) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<UnionQunyingTimeInfo>> dataMap = new ConcurrentHashMap<>();
        if(activityIdList.size() <= 0){
            return dataMap;
        }
        String sql = "select * from t_s_activity_qunying_time where activityId in(" + StringUtils.listToString(activityIdList, ",") +
                ") order by timeId";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionQunyingTimeInfo data = initUnionQunyingTimeInfo(rs);
                if(!dataMap.containsKey(data.getActivityId())){
                    dataMap.put(data.getActivityId(), new ArrayList<>());
                }
                dataMap.get(data.getActivityId()).add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    private UnionQunyingTimeInfo initUnionQunyingTimeInfo(ResultSet rs) throws SQLException {
        UnionQunyingTimeInfo data = new UnionQunyingTimeInfo();
        data.setActivityId(rs.getInt("activityId"));
        data.setTimeId(rs.getInt("timeId"));
        data.setType(rs.getInt("type"));
        data.setSession(rs.getInt("session"));
        data.setBeginTime(rs.getInt("beginTime"));
        data.setDurTime(rs.getInt("durTime"));
        data.setScore(rs.getString("score"));
        data.setRewardsPresident(rs.getString("rewardsPresident"));
        data.setRewardsMember(rs.getString("rewardsMember"));
        data.setMatchVideo(rs.getInt("matchVideo"));
        data.setParam(rs.getString("param"));
        return data;
    }
}
